package com.googlecode.framework.core.pagination;

import java.util.LinkedHashSet;
import java.util.Set;


public abstract class AbstractOrderablePagination extends AbstractPagination implements OrderablePagination {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -553928309081057798L;
    
    /**
     * 排序条件
     */
    protected Set<Order> orders;
    
    public AbstractOrderablePagination(Paginator paginator) {
        super(paginator);
        this.orders = new LinkedHashSet<Order>();
    }

    /*
     * (non-Javadoc)
     * @see com.googlecode.framework.pagination.OrderablePagination#getOrders()
     */
    @Override
    public Order[] getOrders() {
        return orders.toArray(new Order[orders.size()]);
    }

    public void addOrder(Order order) {
        orders.add(order);
    }
    
    public void addOrder(String name, boolean ascending) {
        orders.add(ascending ? Order.asc(name) : Order.desc(name));
    }
    
    public void removeOrder(Order order) {
        orders.remove(order);
    }
    
    public void removeOrder(String name) {
        for (Order order : orders) {
            if (order.name().equals(name)) {
                orders.remove(order);
            }
        }
    }
}
